package com.sinog.biz.service.business.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sinog.biz.mapper.havexml.AbLieguanMapper;
import com.sinog.biz.mapper.havexml.AbPrisonerMapper;
import com.sinog.biz.mapper.havexml.PrisonerMainMapper;
import com.sinog.biz.mapper.havexml.UploadAppendixFileMapper;
import com.sinog.biz.service.business.SlgInfoService;
import com.sinog.biz.util.CodeUrlUtils;
import com.sinog.biz.util.ConditionUtils;
import com.sinog.biz.util.IdGen;
import com.sinog.biz.util.JdbcUtils;
import com.sinog.biz.util.smlib.Sm4;
import com.sinog.core.domain.AbLieguan;
import com.sinog.core.model.UploadAppendixFile;
import com.sinog.core.model.UserDTO;
import com.sinog.core.util.JsonUtil;
import com.sinog.core.util.MapUtil;
import com.sinog.core.util.PageReturnUtil;
import com.sinog.core.utilbean.Constants;
import com.sinog.core.utilbean.Result;
import com.sinog.core.utilbean.ReturnCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

/**
 * @desc 安置帮教管理双列管
 * @author dh
 * @date 2020-02-16-21:38
 */
@Slf4j
@Service
public class SlgInfoServiceImpl extends ServiceImpl<AbLieguanMapper,AbLieguan> implements SlgInfoService {

    @Autowired
    private RedisTemplate redisTemplate;
    @Resource
    private PrisonerMainMapper prisonerMainMapper;
    @Resource
    private AbLieguanMapper abLieguanMapper;
    @Resource
    private UploadAppendixFileMapper uploadAppendixFileMapper;
    @Resource
    private AbPrisonerMapper abPrisonerMapper;
    @Autowired
    @Qualifier("middleJdbcTemplate")
    private JdbcTemplate middleJdbcTemplate;

    /**
     * 安置帮教管理 - 双列管 - 户籍地 - 列表
     * @param pageIndex 起始页
     * @param pageSize 止页
     * @param queryData 查询条件
     * @return String
     */
    @Override
    public String getSlgHjd(Integer pageIndex,Integer pageSize,String queryData) {
        Map<String,Object> mapTem = MapUtil.requestConversionToMap(JsonUtil.toMap(queryData));
        assert !mapTem.isEmpty();
        Page<Map<String,String>> page = new Page<>(pageIndex + 1,pageSize);
        page = page.setRecords(abPrisonerMapper.getSlgHjd(page,mapTem));
        return PageReturnUtil.pageReturn(page);
    }

    /**
     * 安置帮教管理 - 双列管 - 户籍地 - 司法所双列管申请
     * @param data data
     * @param user user
     * @return Result
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result applySlg(String data,UserDTO user) {
        Map<String,Object> resultMap = JsonUtil.toMap(data);
        // 文件标识
        String mid = resultMap.get("mid").toString();
        // 服刑人员编号
        String priNum = resultMap.get("priNum").toString();
        AbLieguan abLieguan = JsonUtil.toBean(data,AbLieguan.class);
        // 申请司法所id
        abLieguan.setAddareaid(user.getDepartid());
        // 申请司法所名称
        abLieguan.setShenqingname(user.getDepartName());
        // 审批状态，将审批状态改为等待审批
        abLieguan.setShenStatef(Constants.NumberStr.NUM_STR_TWO);
        abLieguan.setShenqingname(user.getDepartName());
        Date date = new Date();
        if(null == abLieguanMapper.selectById(priNum)) {
            String id = IdGen.strid();
            abLieguan.setId(id);
            abLieguan.setCreateUser(user.getAccount());
            abLieguan.setCreateTime(date);
        } else {
            abLieguan.setUpdateTime(date);
            abLieguan.setUpdateUser(user.getAccount());
        }
        UploadAppendixFile uploadAppendixFile2 = uploadAppendixFileMapper.getScInfo(priNum,mid,Constants.NumberStr.NUM_STR_SEVEN);
        if(null != uploadAppendixFile2) {
            abLieguan.setZufanghetongName(uploadAppendixFile2.getFileName());
            abLieguan.setZufanghetongPath(uploadAppendixFile2.getFileUrl());
        }
        UploadAppendixFile uploadAppendixFile3 = uploadAppendixFileMapper.getScInfo(priNum,mid,Constants.NumberStr.NUM_STR_EIGHT);
        if(null != uploadAppendixFile3) {
            abLieguan.setGongzuohetongName(uploadAppendixFile3.getFileName());
            abLieguan.setGongzuohetongPath(uploadAppendixFile3.getFileUrl());
        }
        boolean tag = abLieguan.insertOrUpdate();
        if(tag) {
            //1、户籍地司法所申请基础操作完毕之后，把基础数据表同步到居住地司法所所在省份服务器
            //2、根据犯罪编号查询居住地所在省code，和本服务器code对比，如果不是本省，则同步数据
            //3、申请完成 判断居住地省份是否是本省，如果是什么也不做，如果不是调用目标省份的接口将ab_lieguan和基础数据表同步
            String slgdepartid = abLieguan.getVillid();
            String jzCode = slgdepartid.substring(0,2) + "0000";
            //本系统的本地code
            String localCode = CodeUrlUtils.getLocalCode();
            boolean mark = StringUtils.equals(localCode,jzCode);
            boolean mark1 = StringUtils.equals(jzCode,priNum.substring(0,2) + "0000");
            if(mark || mark1) {
                log.info("双列管-居住地和户籍地一致，无需同步");
                //居住地为本省，无需同步数据
            } else {
                try {
                    //居住地为其他省份，需要同步基础数据
                    String prisonerMainDeleteSql = "delete from prisoner_main where pri_num='" + priNum + "'";
                    String prisaveTempDeleteSql = "delete from prisave_temp where pri_num='" + priNum + "'";
                    String prisonerFxDeleteSql = "delete from prisoner_fx where pri_num='" + priNum + "'";
                    String prisonerHcDeleteSql = "delete from prisoner_hc where pri_num='" + priNum + "'";
                    String prisonerSrDeleteSql = "delete from prisoner_sr where pri_num='" + priNum + "'";
                    String prisonerZnDeleteSql = "delete from prisoner_zn where pri_num='" + priNum + "'";
                    String prisonerZzDeleteSql = "delete from prisoner_zz where pri_num='" + priNum + "'";
                    String uploadAppendixFileDeleteSql = "delete from upload_appendix_file where pri_num='" + priNum + "'";
                    String abLieguanDeleteSql = "delete from ab_lieguan where pri_num='" + priNum + "'";
                    String prisonersJxjsDeleteSql = "delete from prisoners_jxjs where pri_num='" + priNum + "'";
                    String prisonersZyjnDeleteSql = "delete from prisoners_zyjn where pri_num='" + priNum + "'";
                    String prisonerCjpgDeleteSql = "delete from prisoner_cjpg where pri_num='" + priNum + "'";
                    String prisonerXjxxDeleteSql = "delete from prisoner_xjxx where pri_num='" + priNum + "'";
                    String xjReceiptDeleteSql = "delete from xj_receipt where pri_num='" + priNum + "'";
                    String xjReceiptDataDeleteSql = "delete from xj_receipt_data where session_id in (select xjid from xj_receipt where pri_num ='" + priNum + "')";
                    String abPrisonersDeleteSql = "delete from ab_prisoners where pri_num='" + priNum + "'";
                    List<String> prisonerMainInsertSql = JdbcUtils.getInsertSql("prisoner_main","pri_num",priNum,null,null,null,null);
                    List<String> prisaveTempinsertSql = JdbcUtils.getInsertSql("prisave_temp","pri_num",priNum,null,null,null,null);
                    List<String> prisonerFxinsertSql = JdbcUtils.getInsertSql("prisoner_fx","pri_num",priNum,null,null,null,null);
                    List<String> prisonerHcinsertSql = JdbcUtils.getInsertSql("prisoner_hc","pri_num",priNum,null,null,null,null);
                    List<String> prisonerSrinsertSql = JdbcUtils.getInsertSql("prisoner_sr","pri_num",priNum,null,null,null,null);
                    List<String> prisonerZninsertSql = JdbcUtils.getInsertSql("prisoner_zn","pri_num",priNum,null,null,null,null);
                    List<String> prisonerZzinsertSql = JdbcUtils.getInsertSql("prisoner_zz","pri_num",priNum,null,null,null,null);
                    List<String> uploadAppendixFileInsertSql = JdbcUtils.getInsertSql("upload_appendix_file","pri_num",priNum,null,null,null,null);
                    List<String> abLieguanInsertSql = JdbcUtils.getInsertSql("ab_lieguan","pri_num",priNum,null,null,null,null);
                    List<String> prisonersJxjsInsertSql = JdbcUtils.getInsertSql("prisoners_jxjs","pri_num",priNum,null,null,null,null);
                    List<String> prisonersZyjnInsertSql = JdbcUtils.getInsertSql("prisoners_zyjn","pri_num",priNum,null,null,null,null);
                    List<String> prisonerCjpgInsertSql = JdbcUtils.getInsertSql("prisoner_cjpg","pri_num",priNum,null,null,null,null);
                    List<String> prisonerXjxxInsertSql = JdbcUtils.getInsertSql("prisoner_xjxx","pri_num",priNum,null,null,null,null);
                    List<String> xjReceiptInsertSql = JdbcUtils.getInsertSql("xj_receipt","pri_num",priNum,null,null,null,null);
                    List<String> xjReceiptDataInsertSql = JdbcUtils.getInsertSql1("xj_receipt_data","session_id",priNum,null,null,null,null);
                    List<String> abPrisonersInsertSql = JdbcUtils.getInsertSql("ab_prisoners","pri_num",priNum,null,null,null,null);
                    String middleSql1 = JdbcUtils.createMiddleSql(priNum,prisonerMainDeleteSql,Constants.Number.NUM_ZERO,localCode,jzCode);
                    middleJdbcTemplate.update(middleSql1);
                    String middleSql2 = JdbcUtils.createMiddleSql(priNum,prisaveTempDeleteSql,Constants.Number.NUM_ZERO,localCode,jzCode);
                    middleJdbcTemplate.update(middleSql2);
                    String middleSql3 = JdbcUtils.createMiddleSql(priNum,prisonerFxDeleteSql,Constants.Number.NUM_ZERO,localCode,jzCode);
                    middleJdbcTemplate.update(middleSql3);
                    String middleSql4 = JdbcUtils.createMiddleSql(priNum,prisonerHcDeleteSql,Constants.Number.NUM_ZERO,localCode,jzCode);
                    middleJdbcTemplate.update(middleSql4);
                    String middleSql5 = JdbcUtils.createMiddleSql(priNum,prisonerSrDeleteSql,Constants.Number.NUM_ZERO,localCode,jzCode);
                    middleJdbcTemplate.update(middleSql5);
                    String middleSql6 = JdbcUtils.createMiddleSql(priNum,prisonerZnDeleteSql,Constants.Number.NUM_ZERO,localCode,jzCode);
                    middleJdbcTemplate.update(middleSql6);
                    String middleSql7 = JdbcUtils.createMiddleSql(priNum,prisonerZzDeleteSql,Constants.Number.NUM_ZERO,localCode,jzCode);
                    middleJdbcTemplate.update(middleSql7);
                    String middleSql8 = JdbcUtils.createMiddleSql(priNum,uploadAppendixFileDeleteSql,Constants.Number.NUM_ZERO,localCode,jzCode);
                    middleJdbcTemplate.update(middleSql8);
                    String middleSql9 = JdbcUtils.createMiddleSql(priNum,abLieguanDeleteSql,Constants.Number.NUM_ZERO,localCode,jzCode);
                    middleJdbcTemplate.update(middleSql9);
                    String middleSql10 = JdbcUtils.createMiddleSql(priNum,prisonersJxjsDeleteSql,Constants.Number.NUM_ZERO,localCode,jzCode);
                    middleJdbcTemplate.update(middleSql10);
                    String middleSql11 = JdbcUtils.createMiddleSql(priNum,prisonersZyjnDeleteSql,Constants.Number.NUM_ZERO,localCode,jzCode);
                    middleJdbcTemplate.update(middleSql11);
                    String middleSql12 = JdbcUtils.createMiddleSql(priNum,prisonerCjpgDeleteSql,Constants.Number.NUM_ZERO,localCode,jzCode);
                    middleJdbcTemplate.update(middleSql12);
                    String middleSql13 = JdbcUtils.createMiddleSql(priNum,prisonerXjxxDeleteSql,Constants.Number.NUM_ZERO,localCode,jzCode);
                    middleJdbcTemplate.update(middleSql13);
                    String middleSql14 = JdbcUtils.createMiddleSql(priNum,xjReceiptDeleteSql,Constants.Number.NUM_ZERO,localCode,jzCode);
                    middleJdbcTemplate.update(middleSql14);
                    String middleSql15 = JdbcUtils.createMiddleSql(priNum,xjReceiptDataDeleteSql,Constants.Number.NUM_ZERO,localCode,jzCode);
                    middleJdbcTemplate.update(middleSql15);
                    String middleSql16 = JdbcUtils.createMiddleSql(priNum,abPrisonersDeleteSql,Constants.Number.NUM_ZERO,localCode,jzCode);
                    middleJdbcTemplate.update(middleSql16);
                    for(String sql : prisonerMainInsertSql) {
                        String isql = JdbcUtils.createMiddleSql(priNum,sql,Constants.Number.NUM_ZERO,localCode,jzCode);
                        middleJdbcTemplate.update(isql);
                    }
                    for(String sql : prisaveTempinsertSql) {
                        String isql = JdbcUtils.createMiddleSql(priNum,sql,Constants.Number.NUM_ZERO,localCode,jzCode);
                        middleJdbcTemplate.update(isql);
                    }
                    for(String sql : prisonerFxinsertSql) {
                        String isql = JdbcUtils.createMiddleSql(priNum,sql,Constants.Number.NUM_ZERO,localCode,jzCode);
                        middleJdbcTemplate.update(isql);
                    }
                    for(String sql : prisonerHcinsertSql) {
                        String isql = JdbcUtils.createMiddleSql(priNum,sql,Constants.Number.NUM_ZERO,localCode,jzCode);
                        middleJdbcTemplate.update(isql);
                    }
                    for(String sql : prisonerSrinsertSql) {
                        String isql = JdbcUtils.createMiddleSql(priNum,sql,Constants.Number.NUM_ZERO,localCode,jzCode);
                        middleJdbcTemplate.update(isql);
                    }
                    for(String sql : prisonerZninsertSql) {
                        String isql = JdbcUtils.createMiddleSql(priNum,sql,Constants.Number.NUM_ZERO,localCode,jzCode);
                        middleJdbcTemplate.update(isql);
                    }
                    for(String sql : prisonerZzinsertSql) {
                        String isql = JdbcUtils.createMiddleSql(priNum,sql,Constants.Number.NUM_ZERO,localCode,jzCode);
                        middleJdbcTemplate.update(isql);
                    }
                    for(String sql : uploadAppendixFileInsertSql) {
                        String isql = JdbcUtils.createMiddleSql(priNum,sql,Constants.Number.NUM_ZERO,localCode,jzCode);
                        middleJdbcTemplate.update(isql);
                    }
                    for(String sql : abLieguanInsertSql) {
                        String isql = JdbcUtils.createMiddleSql(priNum,sql,Constants.Number.NUM_ZERO,localCode,jzCode);
                        middleJdbcTemplate.update(isql);
                    }
                    for(String sql : prisonersJxjsInsertSql) {
                        String isql = JdbcUtils.createMiddleSql(priNum,sql,Constants.Number.NUM_ZERO,localCode,jzCode);
                        middleJdbcTemplate.update(isql);
                    }
                    for(String sql : prisonersZyjnInsertSql) {
                        String isql = JdbcUtils.createMiddleSql(priNum,sql,Constants.Number.NUM_ZERO,localCode,jzCode);
                        middleJdbcTemplate.update(isql);
                    }
                    for(String sql : prisonerCjpgInsertSql) {
                        String isql = JdbcUtils.createMiddleSql(priNum,sql,Constants.Number.NUM_ZERO,localCode,jzCode);
                        middleJdbcTemplate.update(isql);
                    }
                    for(String sql : prisonerXjxxInsertSql) {
                        String isql = JdbcUtils.createMiddleSql(priNum,sql,Constants.Number.NUM_ZERO,localCode,jzCode);
                        middleJdbcTemplate.update(isql);
                    }
                    for(String sql : xjReceiptInsertSql) {
                        String isql = JdbcUtils.createMiddleSql(priNum,sql,Constants.Number.NUM_ZERO,localCode,jzCode);
                        middleJdbcTemplate.update(isql);
                    }
                    for(String sql : xjReceiptDataInsertSql) {
                        String isql = JdbcUtils.createMiddleSql(priNum,sql,Constants.Number.NUM_ZERO,localCode,jzCode);
                        middleJdbcTemplate.update(isql);
                    }
                    for(String sql : abPrisonersInsertSql) {
                        String isql = JdbcUtils.createMiddleSql(priNum,sql,Constants.Number.NUM_ZERO,localCode,jzCode);
                        middleJdbcTemplate.update(isql);
                    }
                } catch(DataAccessException e) {
                    log.error("发生异常:",e);
                }
            }
        }
        return Result.success("保存成功");
    }

    /**
     * 根据犯罪编号查询ablieguan实体
     * @param priNum priNum
     * @return String
     */
    @Override
    public String getApplyInfo(String priNum) {
        AbLieguan abLieguan = abLieguanMapper.selectById(priNum);
        Map<String,Object> map = new HashMap<>(16);
        map.put("shenDate",null == abLieguan ? "" : abLieguan.getShenDate());
        map.put("shenReason",null == abLieguan ? "" : abLieguan.getShenReason());
        return JsonUtil.toText(map);
    }

    /**
     * 安置帮教管理 - 双列管 - 居住地 - 列表
     * @param pageIndex pageIndex
     * @param pageSize pageSize
     * @param queryData queryData
     * @param user user
     * @return String
     */
    @Override
    public String getSlgJzd(Integer pageIndex,Integer pageSize,String queryData,UserDTO user) {
        Map<String,Object> mapTem = MapUtil.requestConversionToMap(JsonUtil.toMap(queryData));
        if(mapTem.isEmpty()) {
            return null;
        }
        List<String> qitaKey = null;
        String qitaKeyStr = (String)mapTem.get("qitaKey");
        if(StringUtils.isNotBlank(qitaKeyStr)) {
            String[] split = qitaKeyStr.split(",");
            qitaKey = Arrays.asList(split);
        }
        mapTem.put("qitaKey",qitaKey);
        if(ObjectUtils.isNotEmpty(mapTem.get("shenKey"))) {
            mapTem.put("shenKey",Sm4.decryptEcb(mapTem.get("shenKey").toString()));
        }
        mapTem.put("departid",user.getDepartid());
        boolean mark = mapTem.containsKey("sqend") && null != mapTem.get("sqend") && !StringUtils.isEmpty((String)mapTem.get("sqend"));
        if(mark) {
            String sqend = (String)mapTem.get("sqend");
            sqend = sqend.replace("00:00:00","23:59:59");
            mapTem.put("sqend",sqend);
        }
        mapTem.put("level",user.getLevel());

        Page<Map<String,String>> page = new Page<>(pageIndex + 1,pageSize);
        page = page.setRecords(prisonerMainMapper.getSlgJzd(page,mapTem));
        //封装搜索条件
        mapTem.put("total",page.getTotal());
        CompletableFuture.runAsync(() -> {
            log.info("工作台- 居住地-异步条件进入redis {}:"+Thread.currentThread().getName());
            ConditionUtils.searchConditions(mapTem,mapTem.get("account").toString(),redisTemplate);
        });
        return PageReturnUtil.pageReturn(page);
    }

    /**
     * 根据犯罪编号查询ablieguan实体部分信息
     * @param priNum 罪犯编号
     * @return String
     */
    @Override
    public String getApproveInfo(String priNum) {
        AbLieguan abLieguan = abLieguanMapper.selectById(priNum);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        Map<String,Object> map = new HashMap<>(16);
        map.put("shenqingname",null == abLieguan ? "" : abLieguan.getShenqingname());
        map.put("shenDate",null == abLieguan ? "" : sdf.format(abLieguan.getShenDate()));
        map.put("shenReason",null == abLieguan ? "" : abLieguan.getShenReason());
        map.put("shenStatef",null == abLieguan ? "" : abLieguan.getShenStatef());
        map.put("reason",null == abLieguan ? "" : abLieguan.getReason());
        map.put("approvalName",null == abLieguan ? "" : abLieguan.getApprovalName());
        map.put("approvalTel",null == abLieguan ? "" : abLieguan.getApprovalTel());
        return JsonUtil.toText(map);
    }

    /**
     * 安置帮教管理 - 双列管 - 居住地 - 司法所审批通过
     * @param data data
     * @param user user
     * @return Result
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result saveJzdAppro(String data,UserDTO user) {
        AbLieguan abLieguan = new AbLieguan();
        Map<String,Object> map = JsonUtil.toMap(data);
        String pid = map.get("pid").toString();
        Date date = new Date();
        abLieguan.setPriNum(pid);
        abLieguan.setShenStatef(map.get("shenStatef").toString());
        abLieguan.setDoTime(date);
        abLieguan.setReason(map.get("reason").toString());
        abLieguan.setApprovalName(map.get("approvalName").toString());
        abLieguan.setApprovalTel(map.get("approvalTel").toString());
        abLieguan.setUpdateTime(date);
        abLieguan.setUpdateUser(user.getAccount());
        String slgState = Constants.NumberStr.NUM_STR_TWO;
        if(Constants.NumberStr.NUM_STR_ONE.equals(map.get(Constants.StrFinalValue.SHENSTATEF))) {
            slgState = Constants.NumberStr.NUM_STR_ONE;
        }
        abLieguan.setShenState(slgState);
        if(null == abLieguanMapper.selectById(abLieguan.getPriNum())) {
            abLieguan.setId(IdGen.strid());
        }
        boolean tag = abLieguan.insertOrUpdate();
        try {
            if(!tag) {
                throw new RuntimeException("强制回滚");
            }
        } catch(RuntimeException e) {
            log.error("发生异常:",e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        if(tag) {
            return Result.success();
        } else {
            return Result.failure(ReturnCode.SAVE_FAILED);
        }
    }

    /**
     * 根据犯罪编号和ID查询ablieguan实体部分信息
     * @param priNum priNum
     * @param id id
     * @param user user
     * @return String
     */
    @Override
    public String getDetailsInfo(String priNum,String id,UserDTO user) {
        AbLieguan abLieguan = abLieguanMapper.selectById(priNum);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        Map<String,String> map = new HashMap<>(16);
        if(null != abLieguan) {
            map.put("shenqingname",abLieguan.getShenqingname());
            /*添加了审批司法所名称*/
            map.put("approvalname",abLieguan.getApprovalName());
            map.put("shenDate",null == abLieguan.getShenDate() ? "" : sdf.format(abLieguan.getShenDate()));
            map.put("shenReason",abLieguan.getShenReason());
            map.put("villid",abLieguan.getAreaname());
            map.put("doTime",null == abLieguan.getDoTime() ? "" : sdf.format(abLieguan.getDoTime()));
            String shenStatef = "";
            switch(abLieguan.getShenStatef()) {
                case "1":
                    shenStatef = "审批通过";
                    break;
                case "2":
                    shenStatef = "等待审批";
                    break;
                case "3":
                    shenStatef = "审批失败";
                    break;
                case "4":
                    shenStatef = "未申请";
                    break;
                /*添加已移除状态*/
                case "5":
                    shenStatef = "已移除";
                    break;
                default:
                    break;
            }
            map.put("shenStatef",shenStatef);
            map.put("reason",abLieguan.getReason());
            map.put("departnameJc",abLieguan.getDepartnameJc());
            map.put("dateJc",null == abLieguan.getDateJc() ? "" : sdf.format(abLieguan.getDateJc()));
            map.put("reasonJc",abLieguan.getReasonJc());
            map.put("pid",priNum);
            map.put("mid",id);
        }
        return JsonUtil.toText(map);
    }

    /**
     * 根据犯罪编号查询ablieguan实体部分信息
     * @param priNum priNum
     * @return Result
     */
    @Override
    public Result getRemoveSlgInfo(String priNum) {
        AbLieguan abLieguan = abLieguanMapper.selectById(priNum);
        if(null != abLieguan) {
            //说明这个双列管已移除
            if(Constants.NumberStr.NUM_STR_FIVE.equals(abLieguan.getShenStatef())) {
                return Result.success();
            } else {
                return Result.failure(ReturnCode.SELECT_FAILED);
            }
        }
        return Result.failure(ReturnCode.SELECT_FAILED);
    }

    /**
     * 安置帮教管理 - 双列管 - 居住地 - 解除双列管
     * @param data data
     * @param user user
     * @return Result
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result removeSlg(String data,UserDTO user) {
        // 更新三个表的双列管状态
        Map<String,Object> resultMap = JsonUtil.toMap(data);
        Map<String,Object> map = new HashMap<>(16);
        Date date = new Date();
        map.put("pid",resultMap.get("pid"));
        map.put("reasonJc",resultMap.get("reasonJc"));
        map.put("dateJc",date);
        map.put("departidJc",user.getDepartid());
        map.put("departnameJc",user.getDepartName());
        map.put("shenState",Constants.NumberStr.NUM_STR_TWO);
        map.put("relieveName",resultMap.get("relieveName"));
        map.put("relieveTel",resultMap.get("relieveTel"));
        //设置双列表审批状态为5,表示被移除
        map.put("shenStatef",Constants.NumberStr.NUM_STR_FIVE);
        map.put("updateTime",date);
        map.put("updateUser",user.getAccount());
        int row1 = abLieguanMapper.removeSlg(map);
        try {
            if(0 >= row1) {
                throw new RuntimeException("强制回滚");
            }
        } catch(RuntimeException e) {
            log.error("发生异常:",e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        if(0 < row1) {
            return Result.success();
        } else {
            return Result.failure(ReturnCode.SAVE_FAILED);
        }
    }

    /**
     * 描述
     * @param priNum priNum
     * @return Result
     */
    @Override
    public Result checkplaceVill(String priNum) {
        Map<String,Object> map = prisonerMainMapper.checkplaceVill(priNum);
        if(null != map && !map.isEmpty()) {
            return Result.success(map);
        }
        return Result.failure(ReturnCode.SELECT_FAILED);
    }
}